/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.cvut.fel.taskpro.data.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * This entity class represent group, which hold students, team and subjects.
 * 
 *
 * @author Tom
 * @version 1.0.0 beta
 */
@Entity
@Table(name = "groupTaskPro")
@NamedQueries({
    @NamedQuery(name = "Parallel.findByKantor", query = "SELECT p FROM GroupTaskPro p WHERE p.kantor = :kantor"),
    @NamedQuery(name = "Group.BySubject", query = "SELECT k FROM GroupTaskPro k where k.subject=:subject AND k.kantor=:kantor")
})
public class GroupTaskPro implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    @ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.REFRESH, CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST})
    private Kantor kantor;
    @ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.REFRESH, CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST})
    private Subject subject;
    private static final long serialVersionUID = 1L;
    @Column(name = "code", nullable = false)
    private String code;
    @ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.REFRESH, CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST})
    private List<Student> mStudents;
    @ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.REFRESH, CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST})
    @OrderBy("dateOfAssignment ASC")
    private List<Assignment> mAssignments;
    @OneToMany(mappedBy = "group", fetch = FetchType.LAZY, cascade = {CascadeType.REFRESH, CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST})
    @OrderBy("name ASC")
    private List<Team> mTeams;
    @Temporal(TemporalType.TIME)
    private Date startTime;
     @Temporal(TemporalType.TIME)
    private Date stopTime;
     private String dayOfTeach;

    public void setCode(String kod) {
        this.code = kod;
    }

    public Kantor getKantor() {
        return kantor;
    }

    public void setKantor(Kantor kantor) {
        this.kantor = kantor;
    }

    public String getDayOfTeach() {
        return dayOfTeach;
    }

    public void setDayOfTeach(String dayOfTeach) {
        this.dayOfTeach = dayOfTeach;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getStopTime() {
        return stopTime;
    }

    public void setStopTime(Date stopTime) {
        this.stopTime = stopTime;
    }

    

  

    /**
     * prida studenta do paralelky
     *
     * @param s - novy student
     */
    public void addStudent(Student s) {
        if (mStudents == null) {
            mStudents = new ArrayList<Student>();
        }
        mStudents.add(s);
        s.addParalelka(this);
        s.addParalelka(this);
    }

    /**
     * prida zadani do paralelky
     *
     * @param z - nove zadani
     */
    public void addAssignment(Assignment z) {
        if (mAssignments == null) {
            mAssignments = new ArrayList<Assignment>();
        }
        mAssignments.add(z);
    }

    public List<Student> getMStudents() {
        return mStudents;
    }

    public void setMStudents(List<Student> mStudents) {
        this.mStudents = mStudents;
    }



    

    public void setmAssignments(List<Assignment> mAssignments) {
        this.mAssignments = mAssignments;
    }

    /**
     * prida novou skupinu do paralelky
     *
     * @param s - nova skupina
     */
    public void addTeam(Team s) {
        if (mTeams == null) {
            mTeams = new ArrayList<Team>();
        }
        mTeams.add(s);
    }

    public void setmTeams(List<Team> mTeams) {
        this.mTeams = mTeams;
    }

    public List<Team> getmTeams() {
        return mTeams;
    }

    public void setSubject(Subject predmet) {
        this.subject = predmet;
//        subject.addParalelka(this);
    }

    public String getCode() {
        return code;
    }

  

    public List<Assignment> getmAssignments() {
        return mAssignments;
    }

    public Subject getSubject() {
        return subject;
    }
    public void removeAssignment(Assignment assignmentToRemove){
        if(mAssignments==null){
            return;     
        }
        mAssignments.remove(assignmentToRemove);
    }
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (int) id;
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof GroupTaskPro)) {
            return false;
        }
        GroupTaskPro other = (GroupTaskPro) object;
        if (this.id != other.id) {
            return false;
        }
        return true;
    }

    public int getId() {
        return id;
    }

    @Override
    public String toString() {
        return "DL.entity.Paralelka[ id=" + id + " kod=" + code + " ]";
    }
}

 
